import 'package:flutter/material.dart';
import 'package:linphone_flutter_example/phone_view_model.dart';
import 'package:provider/provider.dart';

class Login extends StatefulWidget {
  const Login({Key? key}) : super(key: key);

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  var userNameCtrl = TextEditingController();
  var passwordCtrl = TextEditingController();
  var domainCtrl = TextEditingController();

  initText() {
    userNameCtrl.text = "8090";
    passwordCtrl.text = "";
    domainCtrl.text = "";
  }

  @override
  void initState() {
    initText();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Plugin example app'), actions: [
        ]),
        body: Center(
            child: Column(
          children: [
            Text(
                'linphone version: ${context.watch<PhoneViewModel>().platformVersion}\n'),
            TextField(
              autofocus: true,
              controller: userNameCtrl,
              decoration: const InputDecoration(
                  labelText: "用户名", prefixIcon: Icon(Icons.person)),
            ),
            TextField(
              controller: passwordCtrl,
              decoration: const InputDecoration(
                  labelText: "密码", prefixIcon: Icon(Icons.password)),
            ),
            TextField(
              controller: domainCtrl,
              decoration: const InputDecoration(
                  labelText: "域名", prefixIcon: Icon(Icons.domain)),
            ),
            MaterialButton(
                onPressed: () async {
                  context.read<PhoneViewModel>().login(
                      userNameCtrl.text, passwordCtrl.text, domainCtrl.text);
                },
                child: const Text("连接")),
          ],
        )),
      ),
    );
  }
}
